{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "123ec358-4961-44c7-b948-2ca6b527e8db",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "openai.api_key = \"sk-you-key\"\n",
    "from llama_index.agent import OpenAIAgent\n",
    "from llama_index.llms import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62045a7b-6231-419b-8edf-76de9ca00eb7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import and initialize our tool spec\n",
    "from llama_index.tools.zapier.base import ZapierToolSpec\n",
    "\n",
    "zapier_spec = ZapierToolSpec(api_key=\"sk-ak-your-key\")\n",
    "tools = zapier_spec.to_tool_list()\n",
    "# Create the Agent with our tools\n",
    "llm = OpenAI(model=\"gpt-4-0613\")\n",
    "agent = OpenAIAgent.from_tools(tools, verbose=True, llm=llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef64c53f-62d0-46e6-9f75-08d017a5ca24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: list_actions with args: {}\n",
      "Got output: {\"results\": [{\"id\": \"01H5CJQRBW6SS2MPNA5J5PF337\", \"operation_id\": \"github_create_comment_854f54e\", \"description\": \"GitHub: Create Comment\", \"params\": {\"instructions\": \"str\", \"Repo\": \"str\", \"Body\": \"str\", \"Issue_or_Pull_request\": \"str\"}}, {\"id\": \"01H5CJPRJ5ZTBVJ9TM8XB96E56\", \"operation_id\": \"github_create_gist_c6a5b27\", \"description\": \"GitHub: Create Gist\", \"params\": {\"instructions\": \"str\", \"Gist_Filename\": \"str\", \"Content_of_the_Gist\": \"str\"}}, {\"id\": \"01H5CJNNE7EE2S2CNAN1VNRW4Q\", \"operation_id\": \"github_find_issue_a3e79a0\", \"description\": \"GitHub: Find Issue\", \"params\": {\"instructions\": \"str\", \"Search_value\": \"str\", \"State\": \"str\", \"Search_by\": \"str\", \"Repo\": \"str\"}}, {\"id\": \"01H5CJN0H7FFSF1YCCBNAXKCA2\", \"operation_id\": \"github_find_repository_d857bf1\", \"description\": \"GitHub: Find Repository\", \"params\": {\"instructions\": \"str\", \"Repository_Name\": \"str\"}}], \"configuration_link\": \"https://nla.zapier.com/dev/actions/\"}\n",
      "========================\n",
      "Here are the available actions:\n",
      "\n",
      "1. **GitHub: Create Comment**\n",
      "   - ID: 01H5CJQRBW6SS2MPNA5J5PF337\n",
      "   - Parameters: \n",
      "     - Instructions (str)\n",
      "     - Repo (str)\n",
      "     - Body (str)\n",
      "     - Issue_or_Pull_request (str)\n",
      "\n",
      "2. **GitHub: Create Gist**\n",
      "   - ID: 01H5CJPRJ5ZTBVJ9TM8XB96E56\n",
      "   - Parameters: \n",
      "     - Instructions (str)\n",
      "     - Gist_Filename (str)\n",
      "     - Content_of_the_Gist (str)\n",
      "\n",
      "3. **GitHub: Find Issue**\n",
      "   - ID: 01H5CJNNE7EE2S2CNAN1VNRW4Q\n",
      "   - Parameters: \n",
      "     - Instructions (str)\n",
      "     - Search_value (str)\n",
      "     - State (str)\n",
      "     - Search_by (str)\n",
      "     - Repo (str)\n",
      "\n",
      "4. **GitHub: Find Repository**\n",
      "   - ID: 01H5CJN0H7FFSF1YCCBNAXKCA2\n",
      "   - Parameters: \n",
      "     - Instructions (str)\n",
      "     - Repository_Name (str)\n",
      "\n",
      "You can use these actions by providing the required parameters.\n"
     ]
    }
   ],
   "source": [
    "print(agent.chat(\"what actions are available\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7bbd5899-fa8a-4a18-abfb-083abe1fc032",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: natural_language_query with args: {\n",
      "  \"id\": \"01H5CJN0H7FFSF1YCCBNAXKCA2\",\n",
      "  \"params\": {\n",
      "    \"instructions\": \"Find the haliteBot repository\",\n",
      "    \"Repository_Name\": \"haliteBot\"\n",
      "  }\n",
      "}\n",
      "Got output: {\"id\": \"01H5CMJWNTKGBRAMCZX9YQXAY1\", \"action_used\": \"GitHub: Find Repository\", \"input_params\": {\"Repository_Name\": \"haliteBot\"}, \"review_url\": \"https://nla.zapier.com/execution/01H5CMJWNTKGBRAMCZX9YQXAY1/\", \"result\": {\"allow_rebase_merge\": true, \"url\": \"https://api.github.com/repos/ajhofmann/haliteBot\", \"language\": \"Python\", \"size\": 2652, \"archive_url\": \"https://api.github.com/repos/ajhofmann/haliteBot/{archive_format}{/ref}\", \"name\": \"haliteBot\", \"visibility\": \"public\", \"allow_auto_merge\": false, \"full_name\": \"ajhofmann/haliteBot\", \"html_url\": \"https://github.com/ajhofmann/haliteBot\", \"id\": 74611558, \"updated_at\": \"2017-05-03T03:50:32Z\", \"created_at\": \"2016-11-23T20:28:58Z\", \"contents_url\": \"https://api.github.com/repos/ajhofmann/haliteBot/contents/{+path}\", \"blobs_url\": \"https://api.github.com/repos/ajhofmann/haliteBot/git/blobs{/sha}\", \"compare_url\": \"https://api.github.com/repos/ajhofmann/haliteBot/compare/{base}...{head}\", \"downloads_url\": \"https://api.github.com/repos/ajhofmann/haliteBot/downloads\", \"pulls_url\": \"https://api.github.com/repos/ajhofmann/haliteBot/pulls{/number}\", \"git_url\": \"git://...\"}, \"additional_results\": [], \"result_field_labels\": {}, \"status\": \"success\", \"error\": null, \"assistant_hint\": null}\n",
      "========================\n",
      "I found the [haliteBot](https://github.com/ajhofmann/haliteBot) repository. Here are some details:\n",
      "\n",
      "- Full Name: ajhofmann/haliteBot\n",
      "- Language: Python\n",
      "- Size: 2652\n",
      "- Created At: 2016-11-23\n",
      "- Updated At: 2017-05-03\n",
      "- Visibility: Public\n",
      "- URL: [https://github.com/ajhofmann/haliteBot](https://github.com/ajhofmann/haliteBot)\n",
      "\n",
      "You can visit the repository by clicking on the URL.\n"
     ]
    }
   ],
   "source": [
    "print(agent.chat(\"Can you find the haliteBot repo\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04857136-d0e5-467e-9d84-c8695f4a6a00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: natural_language_query with args: {\n",
      "  \"id\": \"01H5CJNNE7EE2S2CNAN1VNRW4Q\",\n",
      "  \"params\": {\n",
      "    \"instructions\": \"Find the latest issues related to search\",\n",
      "    \"Search_value\": \"search\",\n",
      "    \"Repo\": \"haliteBot\"\n",
      "  }\n",
      "}\n",
      "Got output: {\"id\": \"01H5CMKBZAGX0E2YCQB5MARFW0\", \"action_used\": \"GitHub: Find Issue\", \"input_params\": {\"Repo\": \"ajhofmann/haliteBot\", \"State\": \"all\", \"Search_by\": \"title\", \"Search_value\": \"search\"}, \"review_url\": \"https://nla.zapier.com/execution/01H5CMKBZAGX0E2YCQB5MARFW0/\", \"result\": {\"title\": \"Implement A* search instead of Djikstra's\", \"url\": \"https://api.github.com/repos/ajhofmann/haliteBot/issues/1\", \"state\": \"open\", \"html_url\": \"https://github.com/ajhofmann/haliteBot/issues/1\", \"number\": 1, \"id\": 1806018071, \"updated_at\": \"2023-07-15T11:19:45Z\", \"created_at\": \"2023-07-15T10:57:12Z\", \"comments\": 0, \"user__login\": \"ajhofmann\", \"node_id\": \"I_kwDOBHJ7Zs5rpaYX\", \"user__id\": 10040285, \"timeline_url\": \"https://api.github.com/repos/ajhofmann/haliteBot/issues/1/timeline\", \"repository_url\": \"https://api.github.com/repos/ajhofmann/haliteBot\", \"user__html_url\": \"https://github.com/ajhofmann\", \"user__url\": \"https://api.github.com/users/ajhofmann\", \"user__repos_url\": \"https://api.github.com/users/ajhofmann/repos\", \"user__received_events_url\": \"https://api.github.com/users/ajhofmann/received_events\", \"user__following_url\": \"https://api.github.com/users/ajhofmann/following{/other_user}\", \"user__type\": \"User\"}, \"additional_results\": [], \"result_field_labels\": {}, \"status\": \"success\", \"error\": null, \"assistant_hint\": null}\n",
      "========================\n",
      "I found an issue related to search in the haliteBot repository:\n",
      "\n",
      "- Title: [Implement A* search instead of Djikstra's](https://github.com/ajhofmann/haliteBot/issues/1)\n",
      "- State: Open\n",
      "- Created At: 2023-07-15\n",
      "- Updated At: 2023-07-15\n",
      "- Number: 1\n",
      "- User: [ajhofmann](https://github.com/ajhofmann)\n",
      "\n",
      "You can visit the issue by clicking on the title.\n"
     ]
    }
   ],
   "source": [
    "print(agent.chat(\"Can you get the latest few issues about search\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fead3b4e-8307-4316-aace-8eb11a09e994",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sure, here is a basic implementation of the A* search algorithm in Python:\n",
      "\n",
      "```python\n",
      "import heapq\n",
      "\n",
      "def a_star_search(graph, start, goal):\n",
      "    frontier = []\n",
      "    heapq.heappush(frontier, (0, start))\n",
      "    came_from = {}\n",
      "    cost_so_far = {}\n",
      "    came_from[start] = None\n",
      "    cost_so_far[start] = 0\n",
      "    \n",
      "    while frontier:\n",
      "        current = heapq.heappop(frontier)[1]\n",
      "        \n",
      "        if current == goal:\n",
      "            break\n",
      "        \n",
      "        for next in graph.neighbors(current):\n",
      "            new_cost = cost_so_far[current] + graph.cost(current, next)\n",
      "            if next not in cost_so_far or new_cost < cost_so_far[next]:\n",
      "                cost_so_far[next] = new_cost\n",
      "                priority = new_cost + graph.heuristic(goal, next)\n",
      "                heapq.heappush(frontier, (priority, next))\n",
      "                came_from[next] = current\n",
      "    \n",
      "    return came_from, cost_so_far\n",
      "\n",
      "# Reconstruct the path\n",
      "def reconstruct_path(came_from, start, goal):\n",
      "    current = goal\n",
      "    path = []\n",
      "    while current != start:\n",
      "        path.append(current)\n",
      "        current = came_from[current]\n",
      "    path.append(start)\n",
      "    path.reverse()\n",
      "    return path\n",
      "```\n",
      "\n",
      "This code assumes that you have a `graph` object with `neighbors(node)` method that returns neighboring nodes of the given node, `cost(node1, node2)` method that returns the cost of moving from `node1` to `node2`, and `heuristic(goal, node)` method that returns the estimated cost to reach the `goal` from the `node`.\n",
      "\n",
      "Please note that this is a basic implementation and might need to be adjusted based on your specific use case.\n"
     ]
    }
   ],
   "source": [
    "print(agent.chat(\"Can you write an A* search\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b791ee1-0f8a-431d-a6b3-74f467f30ea2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: natural_language_query with args: {\n",
      "  \"id\": \"01H5CJQRBW6SS2MPNA5J5PF337\",\n",
      "  \"params\": {\n",
      "    \"instructions\": \"Post the A* search implementation to the issue\",\n",
      "    \"Repo\": \"haliteBot\",\n",
      "    \"Body\": \"Sure, here is a basic implementation of the A* search algorithm in Python:\\n\\n```python\\nimport heapq\\n\\ndef a_star_search(graph, start, goal):\\n    frontier = []\\n    heapq.heappush(frontier, (0, start))\\n    came_from = {}\\n    cost_so_far = {}\\n    came_from[start] = None\\n    cost_so_far[start] = 0\\n    \\n    while frontier:\\n        current = heapq.heappop(frontier)[1]\\n        \\n        if current == goal:\\n            break\\n        \\n        for next in graph.neighbors(current):\\n            new_cost = cost_so_far[current] + graph.cost(current, next)\\n            if next not in cost_so_far or new_cost < cost_so_far[next]:\\n                cost_so_far[next] = new_cost\\n                priority = new_cost + graph.heuristic(goal, next)\\n                heapq.heappush(frontier, (priority, next))\\n                came_from[next] = current\\n    \\n    return came_from, cost_so_far\\n\\n# Reconstruct the path\\ndef reconstruct_path(came_from, start, goal):\\n    current = goal\\n    path = []\\n    while current != start:\\n        path.append(current)\\n        current = came_from[current]\\n    path.append(start)\\n    path.reverse()\\n    return path\\n```\\n\\nThis code assumes that you have a `graph` object with `neighbors(node)` method that returns neighboring nodes of the given node, `cost(node1, node2)` method that returns the cost of moving from `node1` to `node2`, and `heuristic(goal, node)` method that returns the estimated cost to reach the `goal` from the `node`.\\n\\nPlease note that this is a basic implementation and might need to be adjusted based on your specific use case.\",\n",
      "    \"Issue_or_Pull_request\": \"1\"\n",
      "  }\n",
      "}\n",
      "Got output: {\"id\": \"01H5CMNAS2N8JS6QR70CCNR0PZ\", \"action_used\": \"GitHub: Create Comment\", \"input_params\": {\"Repo\": \"ajhofmann/haliteBot\", \"Body\": \"Sure, here is a basic implementation of the A* search algorithm in Python:\\n\\n```python\\nimport heapq\\n\\ndef a_star_search(graph, start, goal):\\n    frontier = []\\n    heapq.heappush(frontier, (0, start))\\n    came_from = {}\\n    cost_so_far = {}\\n    came_from[start] = None\\n    cost_so_far[start] = 0\\n    \\n    while frontier:\\n        current = heapq.heappop(frontier)[1]\\n        \\n        if current == goal:\\n            break\\n        \\n        for next in graph.neighbors(current):\\n            new_cost = cost_so_far[current] + graph.cost(current, next)\\n            if next not in cost_so_far or new_cost < cost_so_far[next]:\\n                cost_so_far[next] = new_cost\\n                priority = new_cost + graph.heuristic(goal, next)\\n                heapq.heappush(frontier, (priority, next))\\n                came_from[next] = current\\n    \\n    return came_from, cost_so_far\\n\\n# Reconstruct the path\\ndef reconstruct_path(came_from, start, goal):\\n    current = goal\\n    path = []\\n    while current != start:\\n        path.append(current)\\n        current = came_from[current]\\n    path.append(start)\\n    path.reverse()\\n    return path\\n```\\n\\nThis code assumes that you have a `graph` object with `neighbors(node)` method that returns neighboring nodes of the given node, `cost(node1, node2)` method that returns the cost of moving from `node1` to `node2`, and `heuristic(goal, node)` method that returns the estimated cost to reach the `goal` from the `node`.\\n\\nPlease note that this is a basic implementation and might need to be adjusted based on your specific use case.\", \"Issue_or_Pull_request\": \"Implement A* search instead of Djikstra's\"}, \"review_url\": \"https://nla.zapier.com/execution/01H5CMNAS2N8JS6QR70CCNR0PZ/\", \"result\": {\"url\": \"https://api.github.com/repos/ajhofmann/haliteBot/issues/comments/1636740156\", \"body\": \"Sure, here is a basic implementation of the A* search algorithm in Python:\\n\\n```python\\nimport heapq\\n\\ndef a_star_search(graph, start, goal):\\n    frontier = []\\n    heapq.heappush(frontier, (0, start))\\n    came_from = {}\\n    cost_so_far = {}\\n    came_from[start] = None\\n    cost_so_far[start] = 0\\n    \\n    while frontier:\\n        current = heapq.heappop(frontier)[1]\\n        \\n        if current == goal:\\n            break\\n        \\n        for next in graph.neighbors(current):\\n            new_cost = cost_so_far[current] + graph.cost(current, next)\\n            if next not in cost_so_far or new_cost < cost_so_far[next]:\\n                cost_so_far[next] = new_cost\\n                priority = new_cost + graph.heuristic(goal, next)\\n                heapq.heappush(frontier, (priority, next))\\n                came_from[next] = current\\n    \\n    return came_from, cost_so_far\\n\\n# Reconstruct the path\\ndef reconstruct_path(came_from, start, goal):\\n...\"}, \"additional_results\": [], \"result_field_labels\": {}, \"status\": \"success\", \"error\": null, \"assistant_hint\": null}\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='I have posted the A* search implementation to the issue. You can view the comment [here](https://api.github.com/repos/ajhofmann/haliteBot/issues/comments/1636740156).', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"Can post the example implmentation to the issue\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
